package cn.golxen.blog.controller;

import cn.golxen.blog.commons.utils.Result;
import cn.golxen.blog.commons.vo.*;
import cn.golxen.blog.entity.Essay;
import cn.golxen.blog.exception.exceptionClass.AddEssayException;
import cn.golxen.blog.exception.exceptionClass.DeleteEssayException;
import cn.golxen.blog.exception.exceptionClass.UpdateEssayException;
import cn.golxen.blog.service.EssayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping(value = "/essay")
public class EssayController {

    @Autowired
    EssayService essayService;

    @PostMapping(value = "/admin/add")
    public Result addEssay(@RequestBody AddEssayVo addEssayVo){
        int res=essayService.addEssay(addEssayVo);
        if (res!=0)
            return Result.successResult();
        else
            throw new AddEssayException();
    }

    @PostMapping(value = "/getlist")
    public Result getList(@RequestBody PageListVo pageListVo){
//        Long datetime = System.currentTimeMillis();
        ResPageListVo resPageListVo =essayService.getList(pageListVo);
//        System.out.println("后");
//        Long datetime2 = System.currentTimeMillis();
//        System.out.println(datetime2-datetime);
        return Result.successResult(resPageListVo);
    }

    @PostMapping(value = "/admin/delete")
    public Result deleteList(@RequestBody List<String> essayIdList){
        Integer integer=essayService.deleteList(essayIdList);
//        System.out.println("后");
        if (integer!=0)
            return Result.successResult();
        else
            throw new DeleteEssayException();

    }

    @GetMapping(value = "/get/{id}")
    public Result getEssayById(@PathVariable String id){
        Essay essay=essayService.getEssayById(id);
        return Result.successResult(essay);
    }

    @PostMapping(value = "/admin/update")
    public Result updateEssayById(@RequestBody UpdateEssayVo updateEssayVo){
        Integer integer=essayService.updateEssayById(updateEssayVo);
        if (integer!=0)
            return Result.successResult();
        else
            throw new UpdateEssayException();
    }

    @GetMapping(value = "/addview/{id}")
    public Result addViewById(@PathVariable String id){
        Integer integer=essayService.addViewById(id);
        return Result.successResult(integer);
    }

    @GetMapping(value = "/getcountbycategory/{category}")
    public Result getCount(@PathVariable(value = "category") String category){
        Integer integer=essayService.getCountOfCategory(category);
        return Result.successResult(integer);
    }

    @GetMapping(value = "/getset/bycategory/{category}")
    public Result getEssaySetByCategory(@PathVariable(value = "category") String category){
        Set<Essay> essaySet=essayService.getEssaySetByCategory(category);
        return Result.successResult(essaySet);
    }

    @GetMapping(value = "/getset/bylabel/{label}")
    public Result getEssaySetByLabel(@PathVariable(value = "label") String label){

        Set<Essay> essaySet=essayService.getEssaySetByLabel(label);
        return Result.successResult(essaySet);
    }

    @GetMapping(value = "/getallcount")
    public Result getCountOfEssayCategoryLabel(){
        GetAllCount getAllCount=essayService.getAllCount();
        return Result.successResult(getAllCount);
    }

    @GetMapping(value = "/getlabel/{essayId}")
    public Result getLabelByEssayId(@PathVariable(value = "essayId") String essayId){
        Set<String> labelSet=essayService.getLabelByEssayId(essayId);
        return Result.successResult(labelSet);
    }

    @PostMapping(value = "/getlabel/byEssayIdSet")
    public Result getLabelByEssayIdSet(@RequestBody Set<String> essayIdSet){
        Set<ResLabelByEssayIdSetVo> resLabelByEssayIdSetVo=essayService.getLabelByEssayIdSet(essayIdSet);
        return Result.successResult(resLabelByEssayIdSetVo);
    }

    @GetMapping(value = {"/getset/bytitle/{title}","/getset/bytitle/"})
    public Result getEssaySetByTitle(@PathVariable(value = "title",required = false) String title){
        if (title==null)
            title="";
        Set<SearchTitleVo> searchTitleVos= essayService.getEssaySetByTitle(title);
        return Result.successResult(searchTitleVos);
    }

    @GetMapping(value = "/getDate")
    public Result getDateList(){
        List<GetDateListVo> set=essayService.getDateList();
        return Result.successResult(set);
    }

    @GetMapping(value = "/get/byDate/{date}")
    public Result getEssayListByDate(@PathVariable(value = "date") String date){
        List<GetEssayListByDateVo> listByDateVos=essayService.getEssayListByDate(date);
        return Result.successResult(listByDateVos);
    }

}
